<?php
namespace App\Http\Controllers;

use App\Http\Logic\ChainLogic;
use App\Http\Requests\Chain\AddUserChainRequest;
use App\Http\Requests\Chain\QueryUserChainInfoRequest;
use App\Http\Requests\Chain\QueryUserChainRequest;
use Illuminate\Http\JsonResponse;

class ChainController extends BaseController
{
    /**
     * @notes 增加用户关注的链
     * @param AddUserChainRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/14 14:07
     */
    public function addUserChain(AddUserChainRequest $request) : JsonResponse
    {
        $instId = $request->input('instId');
        $instType = $request->input('instType');

        $user_id = auth()->user()->id;

        $res = ChainLogic::addUserChain($instId, $instType, $user_id);

        if(false === $res) {
            return $this->error(ChainLogic::getErrorMessage());
        }

        return $this->success();
    }

    /**
     * @notes 查询用户关注的链
     * @param QueryUserChainRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/14 14:07
     */
    public function queryUserChain(QueryUserChainRequest $request) : JsonResponse
    {
        $type = $request -> input('instType', 'ALL');

        $search = $request -> input('search') ?? '';

        $user_id = auth()->user()->id;

        $list = ChainLogic::queryUserChain($type, $user_id, $search);

        return $this->success($list);
    }

    /**
     * @notes 是否加自选
     * @param QueryUserChainInfoRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/17 11:05
     */
    public function hasUserChainInfo(QueryUserChainInfoRequest $request) : JsonResponse
    {
        $inst_id = $request -> input('instId');

        $user_id = auth()->user()->id;

        $data = [
            'has' => true
        ];

        $res = ChainLogic::queryUserChainInfo($inst_id, $user_id);

        if(false === $res) {
            $data['has'] = false;
        }

        return $this->success($data);
    }
}
